<template>
  <div class="login_container">
    <el-row class="login_row">
      <el-col :span="12"></el-col>
      <el-col class="login_col" :span="7">
        <el-form
          class="login_form"
          ref="ruleFormRef"
          :model="form"
          :rules="rules"
        >
          <el-form-item class="form_title">
            <div>Hello 硅谷甄选</div>
          </el-form-item>

          <el-form-item label="名称" prop="username">
            <el-input :prefix-icon="User" v-model="form.username" clearable />
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input
              :prefix-icon="Lock"
              v-model="form.password"
              type="password"
              show-password
              clearable
            />
          </el-form-item>

          <el-form-item class="form_butBox">
            <el-button
              class="form_but"
              type="primary"
              :loading="loading"
              @click="submitForm(ruleFormRef)"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="5"></el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import useUserStore from '@/store/modules/user.ts'

import { ElNotification } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance, FormRules } from 'element-plus'

import { getTimeMessage } from '@/utils/time.ts'
import { RuleForm } from './type.ts'

const userStore = useUserStore()
const $router = useRouter()

let loading = ref<boolean>(false)
const ruleFormRef = ref<FormInstance>()

const form = reactive<RuleForm>({
  username: 'admin',
  password: '111111',
})
const rules = reactive<FormRules<RuleForm>>({
  username: [
    { required: false, message: '请输入用户名', trigger: 'blur' },
    { min: 5, message: '用户名长度不得小于5', trigger: 'blur' },
  ],
  password: [
    { required: false, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 9, message: '密码长度大于5小于9', trigger: 'blur' },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid) => {
    if (valid) {
      getLogin()
    } else {
      ElNotification({
        type: 'error',
        message: '用户名或密码格式不正确！',
      })
    }
  })
}

const getLogin = async () => {
  loading.value = true
  try {
    await userStore.userLogin(form)
    ElNotification({
      type: 'success',
      message: '登录成功',
      title: `Hi,${getTimeMessage()}好！`,
    })
    loading.value = false
    $router.push({ path: '/' })
  } catch (error) {
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
.login {
  &_container {
    width: 100%;
    height: 100vh;
    background: url('@/assets/images/loginbg.png') no-repeat;
    background-size: cover;
  }

  &_row {
    height: 100%;
  }

  &_col {
    display: flex;
    align-items: center;
    width: 100%;
    height: 100%;
  }

  &_form {
    box-sizing: border-box;
    flex: 1;
    padding: 50px 30px;
    background: rgb(255 255 255 / 40%);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    border-radius: 4px;
    box-shadow: inset 0 0 6px rgb(255 255 255 / 20%);

    ::v-deep .el-form-item__label {
      width: 44px !important;
    }

    .form_butBox {
      margin-bottom: 0 !important;

      ::v-deep .el-form-item__content {
        margin-left: 44px !important;
      }
    }
  }
}

.form {
  &_title {
    margin-bottom: 40px;

    ::v-deep .el-form-item__content {
      margin-left: 0 !important;
      font-size: 32px;
      color: #337ecc;
    }
  }

  &_but {
    flex: 1;
  }
}
</style>
